﻿Public Class Busqueda_Ingreso

    Dim acceso As New acceso_a_datos
    Dim combo_Turno_ As New combo
    Dim primera_busqueda As Boolean = False


    Private Sub Busqueda_Ingreso_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.combo_Turno_.cadena_conexion = acceso._CadenaConexion
        acceso._tabla = "Turno"

        Me.combo_Turno_.cargar(Me.cmb_turno, Me.acceso.leo_tabla("cod_Turno", "nombre"))
        Me.deshabilitar_Campos()
        Me.carga_grilla()

    End Sub

    'BOTON BUSCAR
    Private Sub btn_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_buscar.Click
        If Me.primera_busqueda = False Then
            Me.btn_buscar.Enabled = True

            Me.time_fecha.Enabled = True
            Me.txt_numero.Enabled = True
            Me.cmb_turno.Enabled = True
            Me.lbl_nueva.Visible = False
            Me.lbl_busq.Visible = True
            Me.time_fecha.Focus()
            Me.primera_busqueda = True
            Me.carga_grilla()
            Me.limpiar_campos()
        Else
            If Me.time_fecha.Text = "  /  /" And Me.txt_numero.Text = "" _
                And Me.cmb_turno.SelectedIndex = -1 Then
                MsgBox("Debe ingresar un dato para realizar la busqueda", vbExclamation, "Atencion")
                Me.time_fecha.Focus()
                Exit Sub
            End If
            Try
                If Not time_fecha.Text = Format(CDate(time_fecha.Text), "dd/mm/yyyy") Then
                    MessageBox.Show("La Fecha ingresada es invalida", "IMPORTANTE", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If
            Catch ex As Exception
                MessageBox.Show("La Fecha ingresada es invalida", "IMPORTANTE", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
                Exit Sub
            End Try
            'POR FECHA
            If Me.time_fecha.Text <> "  /  /" And Me.txt_numero.Text = "" _
                And Me.cmb_turno.SelectedIndex = -1 Then

                Me.lbl_nueva.Visible = True
                Me.lbl_busq.Visible = False
                Me.buscar_por_fecha()
                Me.limpiar_campos()
                Me.deshabilitar_Campos()
                Me.primera_busqueda = False
                Exit Sub
            End If
            'POR NUMERO
            If Me.time_fecha.Text = "  /  /" And Me.txt_numero.Text <> "" _
                And Me.cmb_turno.SelectedIndex = -1 Then

                Me.lbl_nueva.Visible = True
                Me.lbl_busq.Visible = False
                Me.buscar_por_numero()
                Me.limpiar_campos()
                Me.deshabilitar_Campos()

                Me.primera_busqueda = False
                Exit Sub
            End If
            'POR TURNO
            If Me.time_fecha.Text = "  /  /" And Me.txt_numero.Text = "" _
                And Me.cmb_turno.SelectedIndex <> -1 Then

                Me.lbl_nueva.Visible = True
                Me.lbl_busq.Visible = False
                Me.buscar_por_turno()
                Me.limpiar_campos()
                Me.deshabilitar_Campos()

                Me.primera_busqueda = False
                Exit Sub
            End If
            '   ¿HACE FALTA ESTA?
            'POR FECHA Y NUMERO
            '    If Me.time_fecha.Text <> "  /  /" And Me.txt_numero.Text <> "" _
            '        And Me.cmb_turno.SelectedIndex = -1 Then

            '        Me.buscar_por_fecha_numero()
            '        Me.limpiar_campos()
            '        Me.deshabilitar_Campos()

            '        Me.primera_busqueda = False
            '        Exit Sub
            '    End If

            Me.limpiar_campos()
            Me.deshabilitar_Campos()

            Me.primera_busqueda = False
        End If

    End Sub

    'BUSQUEDAS
    'POR FECHA
    Private Sub buscar_por_fecha()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        consulta = "SELECT I.fecha, I.numero_Orden, T.nombre as turno, I.monto_Recaudado "
        consulta += "FROM Ingreso I INNER JOIN Turno T ON I.cod_Turno=T.cod_Turno "
        consulta += "WHERE I.fecha='" & Me.time_fecha.Text & "'"

        tabla = acceso._consulta(consulta)

        Me.grilla_Ingreso.Rows.Clear()

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Ingreso.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), tabla.Rows(c)("numero_Orden"), tabla.Rows(c)("turno"), tabla.Rows(c)("monto_Recaudado"))
            Next

            Me.time_fecha.Text = ""
            Me.time_fecha.Focus()
            Exit Sub
        Else
            MsgBox("No se encontro ningun Ingreso con la fecha especificada", vbExclamation, "Importante")
            Me.time_fecha.Text = ""
            Me.time_fecha.Focus()
            Me.carga_grilla()
        End If
    End Sub

    'POR NUMERO DE ORDEN
    Private Sub buscar_por_numero()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        consulta = "SELECT I.fecha, I.numero_Orden, T.nombre as turno, I.monto_Recaudado "
        consulta += "FROM Ingreso I INNER JOIN Turno T ON I.cod_Turno=T.cod_Turno "
        consulta += "WHERE I.numero_Orden = " & Me.txt_numero.Text

        tabla = acceso._consulta(consulta)

        Me.grilla_Ingreso.Rows.Clear()

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Ingreso.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), tabla.Rows(c)("numero_Orden"), tabla.Rows(c)("turno"), tabla.Rows(c)("monto_Recaudado"))
            Next

            Me.txt_numero.Text = ""
            Me.txt_numero.Focus()
            Exit Sub
        Else
            MsgBox("No se encontro ningun Ingreso con el numero de orden especificada", vbExclamation, "Importante")
            Me.txt_numero.Text = ""
            Me.txt_numero.Focus()
            Me.carga_grilla()
        End If
    End Sub

    'POR TURNO
    Private Sub buscar_por_turno()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        consulta = "SELECT I.fecha, I.numero_Orden, T.nombre as turno, I.monto_Recaudado "
        consulta += "FROM Ingreso I INNER JOIN Turno T ON I.cod_Turno=T.cod_Turno "
        consulta += "WHERE I.cod_Turno=" & Me.cmb_turno.SelectedValue

        tabla = acceso._consulta(consulta)

        Me.grilla_Ingreso.Rows.Clear()

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Ingreso.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), tabla.Rows(c)("numero_Orden"), tabla.Rows(c)("turno"), tabla.Rows(c)("monto_Recaudado"))
            Next


            Me.cmb_turno.Focus()
            Exit Sub
        Else
            MsgBox("No se encontro ningun Ingreso que coincida con el turno especificado", vbExclamation, "Importante")

            Me.cmb_turno.Focus()
            Me.carga_grilla()
        End If
    End Sub

    '¿HACE FALTA ESTA?
    'POR FECHA Y NUMERO DE ORDEN
    'Private Sub buscar_por_fecha_numero()
    '    Dim tabla As New Data.DataTable
    '    Dim consulta As String = ""
    '    Dim c As Integer

    '    consulta = "SELECT * FROM Ingreso"
    '    consulta += "WHERE fecha = " & Me.time_fecha.Text
    '    consulta += "AND numero_Orden =" & Me.txt_numero.Text

    '    tabla = acceso._consulta(consulta)

    '    Me.grilla_Ingreso.Rows.Clear()

    '    If tabla.Rows.Count <> 0 Then
    '        For c = 0 To tabla.Rows.Count - 1
    '            Me.grilla_Ingreso.Rows.Add(tabla.Rows(c)("fecha"), tabla.Rows(c)("numero_Orden"), tabla.Rows(c)("cod_turno"), tabla.Rows(c)("monto_Recaudado"))
    '        Next

    '        Me.time_fecha.Text = ""
    '        Me.time_fecha.Focus()
    '        Me.txt_numero.Text = ""
    '        Exit Sub
    '    Else
    '        MsgBox("No se encontro ningun Egreso con esa fecha y numero de orden", vbExclamation, "Importante")
    '        Me.time_fecha.Text = ""
    '        Me.txt_numero.Text = ""
    '        Me.time_fecha.Focus()
    '        Me.carga_grilla()
    '    End If

    'End Sub

    Private Sub deshabilitar_Campos()

        'campos
        Me.time_fecha.Enabled = False
        Me.txt_numero.Enabled = False
        Me.cmb_turno.Enabled = False
        Me.cmb_turno.SelectedIndex = -1

        'botones
        Me.btn_buscar.Enabled = True


    End Sub

    Private Sub habilitar_Campos()

        'campos
        Me.time_fecha.Enabled = True
        Me.txt_numero.Enabled = True
        Me.cmb_turno.Enabled = True

        'botones
        Me.btn_buscar.Enabled = True

    End Sub

    Private Sub limpiar_campos()

        'limpio campos en genral
        Me.time_fecha.Text = ""
        Me.txt_numero.Text = ""
        Me.cmb_turno.SelectedIndex = -1


    End Sub

    'CARGAMOS GRILLA
    Private Sub carga_grilla()
        acceso._tabla = "Ingreso"
        Dim tabla As New Data.DataTable
        Dim txt_sql As String = ""
        Dim c As Integer = 0

        'metodo para insertar en la tabla ingreso.. referente a la planilla de Ingreso..
        txt_sql = "SELECT I.fecha, I.numero_Orden,  T.nombre as nom, I.monto_Recaudado"
        txt_sql += " FROM Ingreso I INNER JOIN Turno T ON I.cod_turno = T.cod_Turno"

        'guardo los datos de la consulta en la tabla
        tabla = acceso._consulta(txt_sql)

        Me.grilla_Ingreso.Rows.Clear()

        For c = 0 To tabla.Rows.Count - 1
            Me.grilla_Ingreso.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), tabla.Rows(c)("numero_Orden"), tabla.Rows(c)("nom"), tabla.Rows(c)("monto_Recaudado"))
        Next

    End Sub

    Private Sub btn_volver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_volver.Click
        Close()

    End Sub

    Private Sub Busqueda_Ingreso_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Dispose()
    End Sub
End Class